//
//  EduBtnBackBiew.m
//  fp
//
//  Created by xingshuai on 2020/11/15.
//

#import "EduBtnBackBiew.h"

#import "CYAnyCornerRadiusUtil.h"

@implementation EduBtnBackBiew

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //切圆角
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    CornerRadii cornerRadii = CornerRadiiMake(14, 3, 3, 14);
    CGPathRef path = CYPathCreateWithRoundedRect(self.bounds,cornerRadii);
    shapeLayer.path = path;
    CGPathRelease(path);
    self.layer.mask = shapeLayer;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    //内侧线
    [self drawLineWithContent:context];
}

- (void)drawLineWithContent:(CGContextRef)context {
    //画线
    const CGFloat minX = CGRectGetMinX(self.bounds);
    const CGFloat minY = CGRectGetMinY(self.bounds);
    const CGFloat maxX = CGRectGetMaxX(self.bounds);
    const CGFloat maxY = CGRectGetMaxY(self.bounds);
    
    CGFloat borderTopLeftRadius = 8;
    CGFloat borderTopRightRadius = 2;
    CGFloat borderBottomLeftRadius = 2;
    CGFloat borderBottomRightRadius = 8;
    
    const CGFloat topLeftCenterX = minX +  borderTopLeftRadius;
    const CGFloat topLeftCenterY = minY + borderTopLeftRadius;
    
    const CGFloat topRightCenterX = maxX - borderTopRightRadius;
    const CGFloat topRightCenterY = minY + borderTopRightRadius;
    
    const CGFloat bottomLeftCenterX = minX +  borderBottomLeftRadius;
    const CGFloat bottomLeftCenterY = maxY - borderBottomLeftRadius;
    
    const CGFloat bottomRightCenterX = maxX -  borderBottomRightRadius;
    const CGFloat bottomRightCenterY = maxY - borderBottomRightRadius;
    
    CGFloat lineW = 0.5;
    CGFloat topSpaceY = (1 + lineW)/2;
    CGFloat rightSpaceX = (1 + lineW)/2;
    CGFloat bottomSpaceY = (1 + lineW)/2;
    CGFloat leftSpaceX = (1 + lineW)/2;
    
    borderTopLeftRadius -=  topSpaceY;
    borderTopRightRadius -=  rightSpaceX;
    borderBottomLeftRadius -=  bottomSpaceY;
    borderBottomRightRadius -=  leftSpaceX;
    
    
    //黄色
    UIColor *color = [UIColor clearColor];
    //1.0 顶左 画圆弧
    drawLineArc(context, color.CGColor, lineW, CGPointMake(topLeftCenterX, topLeftCenterY), borderTopLeftRadius, M_PI, 3 * M_PI_2, NO);
    //2.0 顶部划线
    drawLine(context, color.CGColor, lineW, CGPointMake(topLeftCenterX, topSpaceY), CGPointMake(topRightCenterX, topSpaceY));
    //3.0 顶右 画圆弧
    drawLineArc(context, color.CGColor, lineW, CGPointMake(topRightCenterX, topRightCenterY), borderTopRightRadius, 3 * M_PI_2, 0, NO);
    //4.0 右部划线
    drawLine(context, color.CGColor, lineW, CGPointMake(maxX - rightSpaceX, topRightCenterY), CGPointMake(maxX - rightSpaceX, bottomRightCenterY));
    //5.0 底右 画圆弧
    drawLineArc(context, color.CGColor, lineW, CGPointMake(bottomRightCenterX, bottomRightCenterY), borderBottomRightRadius, 0, M_PI_2, NO);
    //6.0 底部划线
    drawLine(context, color.CGColor, lineW, CGPointMake(bottomRightCenterX, maxY - bottomSpaceY), CGPointMake(bottomLeftCenterX, maxY - bottomSpaceY));
    
    //7.0 底左 画圆弧
    drawLineArc(context, color.CGColor, lineW, CGPointMake(bottomLeftCenterX, bottomLeftCenterY), borderBottomLeftRadius, M_PI_2, M_PI, NO);
    
    //8.0 左边划线
    drawLine(context, color.CGColor, lineW, CGPointMake(minX + leftSpaceX, bottomLeftCenterY), CGPointMake(minX + leftSpaceX, topLeftCenterY));
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
